﻿from django.db import models

#this site user
class User(models.Model):
    login = models.CharField(max_length=50)
    password_hash = models.CharField(max_length=255)
    name = models.CharField(max_length=255)
    surname = models.CharField(max_length=255)
    paternal = models.CharField(max_length=255)
    role = models.CharField(max_length=50)
    phone = models.CharField(max_length=50)
    email = models.EmailField(blank=True)
    is_admin = models.BooleanField(default=False)
    def __str__(self):
        return self.login
        
class AutoModel(models.Model):
    brand = models.CharField(max_length=50)
    model = models.CharField(max_length=50)
    year = models.IntegerField(default=2015)
    
    guarantee_months = models.IntegerField(default=24)
    guarantee_haul_km = models.IntegerField(default=100000)
    
    full_service_schedule_haul_km = models.IntegerField(default=10000)
    full_service_alarm_haul_km = models.IntegerField(default=3000)
    
    oil_service_schedule_haul_km = models.IntegerField(default=3000)
    oil_service_alarm_haul_km = models.IntegerField(default=1000)

    def __str__(self):
        return '%s %s\'%d' % (self.brand, self.model, self.year)
 
class Auto(models.Model):
    model = models.ForeignKey(AutoModel)
    pts_date = models.DateField()
    vin = models.CharField(max_length=50)
    guarantee = models.BooleanField(default=True)
    registered = models.DateTimeField()
    
    gos_znak = models.CharField(max_length=50, blank=True)
    colour = models.CharField(max_length=50, blank=True)
    
    seller = models.CharField(max_length=255, blank=True)
    sell_date = models.DateField(blank=True, null=True)

    full_service_schedule_haul_km = models.IntegerField(default=0, blank=True, null=True)
    full_service_alarm_haul_km = models.IntegerField(default=0, blank=True, null=True)
    
    oil_service_schedule_haul_km = models.IntegerField(default=0, blank=True, null=True)
    oil_service_alarm_haul_km = models.IntegerField(default=0, blank=True, null=True)
    
    owner_name = models.CharField(max_length=50, blank=True)
    owner_surname = models.CharField(max_length=50, blank=True)
    owner_paternal = models.CharField(max_length=50, blank=True)
    owner_phone = models.CharField(max_length=50, blank=True)
    owner_email = models.EmailField(blank=True, null=True)
    owner_address = models.CharField(max_length=255, blank=True)
    user_name = models.CharField(max_length=50, blank=True)
    user_surname = models.CharField(max_length=50, blank=True)
    user_paternal = models.CharField(max_length=50, blank=True)
    user_phone = models.CharField(max_length=50, blank=True)
    user_email = models.EmailField(blank=True, null=True)
    user_address = models.CharField(max_length=255, blank=True)

    comment = models.TextField(blank=True)

    caronline_login = models.CharField(max_length=50)
    caronline_password = models.CharField(max_length=50)
    def __str__(self):
        return '%s %s %s' % (str(self.model), self.vin, str(self.gos_znak))
        
    def getFullServiceScheduleHaulKm(self):
        if self.full_service_schedule_haul_km is not None and self.full_service_schedule_haul_km > 0:
            return self.full_service_schedule_haul_km
        else:
            return self.model.full_service_schedule_haul_km
            
    def getFullServiceAlarmHaulKm(self):
        if self.full_service_alarm_haul_km is not None and self.full_service_alarm_haul_km > 0:
            return self.full_service_alarm_haul_km
        else:
            return self.model.full_service_alarm_haul_km
    
    def getOilServiceScheduleHaulKm(self):
        if self.oil_service_schedule_haul_km is not None and self.oil_service_schedule_haul_km > 0:
            return self.oil_service_schedule_haul_km
        else:
            return self.model.oil_service_schedule_haul_km
    
    def getOilServiceAlarmHaulKm(self):
        if self.oil_service_alarm_haul_km is not None and self.oil_service_alarm_haul_km > 0:
            return self.oil_service_alarm_haul_km
        else:
            return self.model.oil_service_alarm_haul_km
        
        
class ServiceItem(models.Model):
    auto = models.ForeignKey(Auto)
    service_in = models.DateTimeField()
    service_out = models.DateTimeField(null=True, blank=True)
    
    haul_km = models.IntegerField(default=0)
    
    is_oil_service = models.BooleanField(default=False)
    is_planned_service = models.BooleanField(default=False)
    is_finished = models.BooleanField(default=False)
    comment_user = models.TextField(blank=True)
    comment_master = models.TextField(blank=True)
    def __str__(self):
        return '%d %s' % (self.id, str(self.auto))